package com.tm.media.login.controller;

import com.tm.media.utils.utils.Result;
import com.tm.media.login.service.UserLoginService;
import com.tm.media.model.dtos.ResponseResult;
import com.tm.media.model.entity.AdUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("login")
@CrossOrigin
public class UserLoginController {
    @Autowired
    private UserLoginService userLoginService;
    /**
     * 查看用户
     * @return
     */
    @GetMapping("selectUser")
    public Result selectUser(){
        List<AdUser> userEntity= userLoginService.selectUser();
        return Result.SUCCESS(userEntity);
    }
    @GetMapping("in")
    public Result login(String yanzhangma, String name, String password, HttpServletRequest request){
        Result result = userLoginService.login(yanzhangma, name, password, request);
        return Result.SUCCESS(result);
    }
    /**
     * 新增用户
     * @param adUser
     * @return
     */
    @PostMapping("insertUser")
    public ResponseResult insertUser(@RequestBody AdUser adUser){
        int flag= userLoginService.insertUser(adUser);
        if (flag==0){
            return ResponseResult.errorResult(500,"新增失败，用户名重复");
        }
        return ResponseResult.okResult(200,"新增成功");
    }

}